<!DOCTYPE html>
<html lang=zh-CN>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta property="og:description" content="samuel-24276的博客">
    <meta property="og:type" content="website">
    <meta name="description" content="samuel-24276的博客">
    <meta name="keyword"  content="aircloud">
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">

    <title>
        
        工厂模式 - samuel-24276的博客 | samuel-24276&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/myblog/css/aircloud.css">

    
<link rel="stylesheet" href="/myblog/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_28hi1hpxx24.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 7.1.1"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> Like what you do, do what you like </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/myblog/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>samuel-24276</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/myblog/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/myblog/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/myblog/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/myblog/collect/">
                    <i class="iconfont icon-shoucang1"></i>
                    <span>收藏</span>
                </a>
            </li>
            <li >
                <a href="/myblog/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-text">简单工厂模式</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%8D%E6%9D%82%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F-%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-text">复杂工厂模式(工厂模式)</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%89%B9%E7%82%B9"><span class="toc-text">特点</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-text">抽象工厂模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%89%B9%E7%82%B9-1"><span class="toc-text">特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E8%80%85%E5%8C%BA%E5%88%AB%EF%BC%9A"><span class="toc-text">三者区别：</span></a></li></ol></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-bg" id="search-bg"></div>
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> Like what you do, do what you like </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        工厂模式
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2022-04-16 19:45:15</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/myblog/tags/#设计模式" title="设计模式">设计模式</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <blockquote>
<p><strong>设计模式的持久层对应抽象接口，变化层对应抽象接口的具体实现</strong>。</p>
<p>Qt信号槽有三种实现方式：标准连接，函数指针，Lambda表达式。后两种的实现中并没有将槽函数基类声明为抽象基类，而是将槽函数基类声明为普通基类，说明Qt没有采用C++传统的多态机制，这取决于虚表的查询效率。<br>虚表基于绝对位置进行索引，同时包括虚表产生的大量内存消耗，Qt进行内部实现时优先考虑自身的业务需求和内存效率，所以就不采用工厂设计模式。</p>
</blockquote>
<h1 id="简单工厂模式"><a href="#简单工厂模式" class="headerlink" title="简单工厂模式"></a>简单工厂模式</h1><p>将产品抽象成类IProduct，提供统一&#x2F;公共的抽象接口name()，子类ProductImplA，ProductImplB实现父类IProduct的抽象接口name()。<br>借助C++多态的特性，工厂内持有抽象产品IProduct指针，其Create()方法根据不同参数创建不同的产品</p>
<p>IProduct的子类实现：</p>
<ul>
<li>ProductImplA</li>
<li>ProductImplB</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">IProduct* product;</span><br><span class="line"><span class="keyword">switch</span> (type):</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">        product = <span class="keyword">new</span> <span class="built_in">ProductImplA</span>();</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    case1:</span><br><span class="line">        product = <span class="keyword">new</span> <span class="built_in">ProductImplB</span>();</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="复杂工厂模式-工厂模式"><a href="#复杂工厂模式-工厂模式" class="headerlink" title="复杂工厂模式(工厂模式)"></a>复杂工厂模式(工厂模式)</h1><p>产品和工厂分别抽象为IProduct和IFactory，提供统一&#x2F;公共的抽象接口，子类实现父类的抽象接口。<br>相当于简单工厂的组合。</p>
<p>IProduct的子类实现：</p>
<ul>
<li>ProductImplA</li>
<li>ProductImplB</li>
</ul>
<p>IFactory：</p>
<ul>
<li>FactoryImplA</li>
<li>FactoryImplB</li>
</ul>
<h2 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h2><ol>
<li>用于隔离类对象的使用者和具体类型的耦合关系，面对一个经常变化的类型，紧耦合关系会导致软件的脆弱。</li>
<li>通过面向对象（多态）的手法，将创建具体对象的工作延迟到子类去，从而实现一种非更改操作的扩展策略，较好的解决了这种紧耦合关系。</li>
<li>解决的是单对象（单个工厂？）的需求变化，要求对象的参数列表相同。</li>
</ol>
<h1 id="抽象工厂模式"><a href="#抽象工厂模式" class="headerlink" title="抽象工厂模式"></a>抽象工厂模式</h1><p>相当于复杂工厂的组合。</p>
<h2 id="特点-1"><a href="#特点-1" class="headerlink" title="特点"></a>特点</h2><ol>
<li>如果没有应对<strong>多种系列对象构建</strong>的变化需求，则没有必要使用抽象工厂。</li>
<li>”系列对象“指的是在某一特定系列下的对象间有相互依赖，或作用的关系， 不同系列的对象之间不能相互依赖。</li>
<li>抽象工厂应对的是：新系列需求的变化，缺点是难应对“新对象”的需求变化</li>
</ol>
<h2 id="三者区别："><a href="#三者区别：" class="headerlink" title="三者区别："></a>三者区别：</h2><blockquote>
<p>简单工厂实现了对具体产品对象的功能调用，只有一个工厂；<br>复杂工厂实现了对具体产品对象创建时的业务隔离，可以多个工厂；<br>抽象工厂实现了对具体复杂工厂对象创建时的业务隔离。<br>业务隔离的目的是降低调用者对象与具体业务对象的耦合度，从而实现具体业务对象的可扩展性。<br>复杂工厂解决单个对象的业务变化，指的是具体的产品对象的需求变化；<br>抽象工厂解决的是系列的需求变化，复杂工厂包含指定的产品对象，产品对象和复杂工厂对象组成一个序列</p>
</blockquote>
<p>以插件加载举例说明： QPluginLoader实际映射对象是QLibrary，每一个具体插件plugin由QPluginLoader加载，一一对应，每一个QPluginLoader对应一个具体的产品对象；又QFactoryLoader类别加载器可以同时拥有多个相同类别的plugin对象，QFactoryLoader对应具体的工厂对象，QFactoryLoader根据QPluginLoader的IID判断插件对象是否属于工厂对象。</p>
<hr>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/myblog/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>
        <div id="lv-container"></div>
        <div class="giscus"></div>
    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/samuel-24276">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://github.com/samuel-24276">samuel-24276&#39;s Page</a></span>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">北京-未备案</a></span>
        <span>/</span>
        
        <span><a href="#">返回顶部</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/myblog/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/myblog/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






    <script type="text/javascript">
       (function() {
           if (typeof LivereTower === 'function') { return; }

           var j, d = document.getElementById('lv-container');

           d.setAttribute('data-id','city');
           d.setAttribute('data-uid' , 'MTAyMC80OTA5OC8yNTU5Mw==');

           j = document.createElement('script');
           j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
           j.async = true;

           d.appendChild(j);
       })();
    </script>
    <noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
    </div>

</html>
